﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDream
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                object nam = e.NewValues["CateName"];
                object b = e.NewValues["flag"];
                Category c = new Category();
                if (nam != null)
                {
                    c.CateName = nam.ToString();
                    if (b != null)
                        c.flag = bool.Parse(b.ToString());
                    else
                        c.flag = false;
                    dx.Categories.InsertOnSubmit(c);
                    dx.SubmitChanges();
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            ASPxGridView1.CancelEdit();
            e.Cancel = true;
        }

        protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                dx.Connection.Open();
                object id = e.Keys["ID"];
                object nam = e.NewValues["CateName"];
                object b = e.NewValues["flag"];
                Category c = new Category();
                c = dx.Categories.Single(p => p.ID == int.Parse(id.ToString()));
                if (nam != null)
                {
                    c.CateName = nam.ToString();
                    if (b != null)
                        c.flag = bool.Parse(b.ToString());
                    else
                        c.flag = false;

                    dx.SubmitChanges();
                }
            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
            ASPxGridView1.CancelEdit();
            e.Cancel = true;
        }

        protected void ASPxGridView1_Load(object sender, EventArgs e)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            try
            {
                var list = from k in dx.Categories
                           select k;
                ASPxGridView1.DataSource = list;
                ASPxGridView1.DataBind();
            }
            catch (Exception) { }
        }
    }
}